/*
 * Copyright (C) 2015 huwentao.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use getContext() file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mokey.core.base;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.mokey.core.annotation.Layout;

import java.util.UUID;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class BaseFragment extends Fragment {
    private final String FRAGTAG = UUID.randomUUID().toString();
    private Unbinder unbinder;

    public BaseFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Layout layout = getClass().getAnnotation(Layout.class);
        if (layout.value() > 0) {
            View view = LayoutInflater.from(getContext()).inflate(layout.value(), container);
            unbinder = ButterKnife.bind(this, view);
            return view;
        } else {
            return super.onCreateView(inflater, container, savedInstanceState);
        }
    }

    /**
     * 通过类名启动Activity
     *
     * @param pClass
     */
    public void openActivity(Class<BaseActivity> pClass) {
        AppManager.get().startActivity(getContext(), pClass);
    }

    /**
     * 通过类名启动Activity，并且含有Bundle数据
     *
     * @param pClass
     * @param pBundle
     */
    public void openActivity(Class<BaseActivity> pClass, Bundle pBundle) {
        AppManager.get().startActivity(getContext(), pClass, pBundle);
        ;
    }

    /**
     * 通过类名启动Activity
     *
     * @param pClass
     */
    public void openActivityForResult(Class<BaseActivity> pClass, int requestCode) {
        AppManager.get().startActivityForResult(getActivity(), pClass, requestCode);
    }

    /**
     * 通过类名启动Activity，并且含有Bundle数据
     *
     * @param pClass
     * @param pBundle
     */
    public void openActivityForResult(Class<BaseActivity> pClass, Bundle pBundle, int requestCode) {
        AppManager.get().startActivityForResult(getActivity(), pClass, requestCode, pBundle);
    }

    public String getFRAGTAG() {
        return FRAGTAG;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (unbinder != null) {
            unbinder.unbind();
        }
    }

}
